export const mySessionStorage = <T>(
  action: string,
  key?: string,
  value?: T,
): T | undefined | null => {
  switch (action) {
    case 'get':
      try {
        if (key) {
          const val = window.sessionStorage.getItem(key);
          if (val === null) return null;
          return JSON.parse(val) as T;
        }
        console.error('Failed to parse sessionStorage data', '请传入key');
      } catch (err) {
        console.error('Failed to parse sessionStorage data', err);
        return null;
      }
      break;
    case 'set':
      if (key && value) {
        window.sessionStorage.setItem(key.toString(), JSON.stringify(value));
      } else {
        console.error(
          'Failed to parse sessionStorage data',
          '请传入key和value',
        );
      }
      break;
    case 'removeItem':
      if (key) {
        window.sessionStorage.removeItem(key.toString());
      }
      break;
    case 'clear':
      window.sessionStorage.clear();
      break;
    default:
      console.error(
        'Failed to parse sessionStorage data',
        '请传入正确的action',
      );
      break;
  }
};
